//
//  LYCosume.m
//  game
//
//  Created by naffan on 12-11-18.
//  Copyright (c) 2012年 naffan. All rights reserved.
//

#import "LYFill.h"
#import "LeYou.h"

@implementation LYFill

//计费API
-(BOOL)billing:(NSString*) serverid
            orderid:(NSString*)orderid
                uid:(NSString*)uid
              money:(NSString*) money
          productid:(NSString*) productid
        productname:(NSString*) productname
{
    lydb = [[LYDb alloc]init];
    NSLog(@"insert orderid:%@",orderid);
    [lydb create_back:@"create table if not exists orderinfo (id integer primary key autoincrement,uid integer,lastlogintime date,orderid integer)"];
    [lydb open];
    [lydb insert_back:[NSString stringWithFormat:@"insert into orderinfo (uid, lastlogintime,orderid)values ('%@','%@','%@')",uid,[NSDate dateWithTimeIntervalSinceNow: +(8*60*60)],orderid]];
    [lydb release];
    SBJSON *parser = [[SBJSON alloc] init];
    LeYou *leyou = [[LeYou alloc]init];
    NSString *url_base = LeYouApiUrlBase;
    NSString* url_api_for_login = [url_base stringByAppendingString:@"payMoney.shtml"];
    NSURL *url = [NSURL URLWithString:url_api_for_login];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    NSString *mac = [leyou md5:[appid stringByAppendingFormat:@"%@%@%@%@%@%@",serverid,orderid,uid,money,productid,PRIVATE_KEY]];
    NSLog(@"[billing] 参数  appid:%@ serverid:%@ orderid:%@ uid:%@ money:%@ productid:%@ PRIVATE_KEY:%@ mac:%@",appid,serverid,orderid,uid,money,productid,PRIVATE_KEY,[appid stringByAppendingFormat:@"%@%@%@%@%@%@",serverid,orderid,uid,money,productid,PRIVATE_KEY]);
    [request setPostValue:appid forKey:@"appid"];
    [request setPostValue:serverid forKey:@"serverid"];
    [request setPostValue:orderid forKey:@"orderid"];
    [request setPostValue:uid forKey:@"uid"];
    [request setPostValue:money forKey:@"money"];
    [request setPostValue:productid forKey:@"productid"];
    [request setPostValue:productname forKey:@"productname"];
    [request setPostValue:mac forKey:@"mac"];
    [request startSynchronous];
    NSString *response = [request responseString];
    NSLog(@"%@",response);
    NSDictionary *statuses = [parser objectWithString:response];
    [leyou release];
    [parser release];
    if([[statuses objectForKey:@"result" ]intValue] == 1)
    {
        return YES;
    }else{
        return NO;
    }
}

-(BOOL)queryorder:(NSString *)orderid
{
    SBJSON *parser = [[SBJSON alloc] init];
    LeYou *leyou = [[LeYou alloc]init];
    NSString *url_base = LeYouApiUrlBase;
    NSString* url_api_for_login = [url_base stringByAppendingString:@"queryOrder.shtml"];
    NSURL *url = [NSURL URLWithString:url_api_for_login];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    NSString* mac = [leyou md5:[orderid stringByAppendingFormat:@"%@",PRIVATE_KEY]];
    NSLog(@"queryorder orderid :%@",orderid);
    NSLog(@"queryorder mac: %@",[orderid stringByAppendingFormat:@"%@",PRIVATE_KEY]);
    [request setPostValue:orderid forKey:@"orderid"];
    [request setPostValue:mac forKey:@"mac"];
    [request startSynchronous];
    NSString *response = [request responseString];
    NSLog(@"queryorder response:%@",response);
    NSDictionary *statuses = [parser objectWithString:response];
    [leyou release];
    [parser release];
    if([[statuses objectForKey:@"result" ]intValue] == 1)
    {
        return YES;
    }else{
        return NO;
    }
}



-(void*)loadFillPage:(UIView*)view
serverid:(NSString*)serverid
orderid:(NSString*) orderid
uid:(NSString*)uid
money:(NSString*)money
productid:(NSString*)productid
productname:(NSString*)productname
{
    LeYou *leyou = [[LeYou alloc]init];
    UIWebView* WebView  = [[ UIWebView   alloc]  initWithFrame: CGRectMake(0, 0,320, 1024)];
    WebView.delegate = view;
    
    NSURL *url = [NSURL URLWithString:LeYouPayUrl];
    NSString *mac = [leyou md5:[appid stringByAppendingFormat:@"%@%@%@%@%@%@",serverid,orderid,uid,money,productid,PRIVATE_KEY]];
    /*
    NSLog(@"appid : %@",appid);
    NSLog(@"serverid : %@",serverid);
    NSLog(@"orderid : %@",orderid);
    NSLog(@"uid : %@",uid);
    NSLog(@"money : %@",money);
    NSLog(@"productid : %@",productid);
    NSLog(@"productname : %@",productname);
    NSLog(@"mac : %@",mac);
     */
    //POST打开页面 关键次：UIWebview post值
    NSString *body = [NSString stringWithFormat: @"appid=%@&serverid=%@&orderid=%@&uid=%@&money=%@&productid=%@&productname=%@&mac=%@",appid,serverid,orderid,uid,money,productid,productname,mac];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
    NSLog(@"come here");
    [WebView loadRequest: request];
    [view addSubview:WebView];
}


@end
